/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaLoteNuevo.java
 *
 * Created on 03/09/2009, 03:35:55 AM
 */

package ac.view;

import ac.entities.Alimento;
import ac.entities.Lote;
import ac.persistence.PersistAlimento;
import ac.persistence.PersistenceFreddy;
import java.util.ArrayList;

/**
 *
 * @author freddy
 */
public class VentanaBusquedaLote extends javax.swing.JFrame {

    ArrayList<Alimento> alimentos = new ArrayList();
    ArrayList<Lote> lotes = new ArrayList();
    PersistenceFreddy persistFreddy = new PersistenceFreddy();
    PersistAlimento persistAlimento = new PersistAlimento();

    /** Creates new form VentanaLoteNuevo */
    public VentanaBusquedaLote() {
        LookAndFeel look = new LookAndFeel();
        initComponents();
        llenarCombo();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        comboAlimentos = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        campoCantidad = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        areaPresentacion = new javax.swing.JTextArea();
        jButton2 = new javax.swing.JButton();
        campoVencimiento = new javax.swing.JTextField();
        campoElabora = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        comboLotes = new javax.swing.JComboBox();
        botonAlimentos = new javax.swing.JButton();
        botonLotes = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Alimentos:");

        comboAlimentos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboAlimentosActionPerformed(evt);
            }
        });

        jLabel2.setText("Cantidad:");

        jLabel3.setText("Fecha vencimiento:");

        jLabel4.setText("Fecha elaboracion:");

        jLabel5.setText("Presentacion:");

        areaPresentacion.setColumns(20);
        areaPresentacion.setRows(5);
        jScrollPane1.setViewportView(areaPresentacion);

        jButton2.setText("Aceptar");

        jLabel6.setText("Lotes:");

        comboLotes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboLotesActionPerformed(evt);
            }
        });

        botonAlimentos.setText("Buscar");
        botonAlimentos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAlimentosActionPerformed(evt);
            }
        });

        botonLotes.setText("Buscar");
        botonLotes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonLotesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(47, 47, 47)
                                .addComponent(campoCantidad, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoVencimiento, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(campoElabora, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE))))
                        .addGap(122, 122, 122))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel6))
                        .addGap(47, 47, 47)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(comboLotes, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(comboAlimentos, 0, 173, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(botonAlimentos)
                            .addComponent(botonLotes))
                        .addGap(49, 49, 49))))
            .addGroup(layout.createSequentialGroup()
                .addGap(170, 170, 170)
                .addComponent(jButton2)
                .addContainerGap(188, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(comboAlimentos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonAlimentos))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(comboLotes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonLotes))
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(campoCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(campoVencimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(campoElabora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(jLabel5))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addGap(39, 39, 39))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void comboAlimentosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboAlimentosActionPerformed
       
    }//GEN-LAST:event_comboAlimentosActionPerformed

    private void comboLotesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboLotesActionPerformed
        
    }//GEN-LAST:event_comboLotesActionPerformed

    private void botonAlimentosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAlimentosActionPerformed
         Alimento alimento = null;
        alimento = alimentos.get(comboAlimentos.getSelectedIndex());
        llenarComboLotes(alimento.getIdAlimento());
    }//GEN-LAST:event_botonAlimentosActionPerformed

    private void botonLotesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonLotesActionPerformed
        Lote lote = null;
        lote = lotes.get(comboLotes.getSelectedIndex());

        campoCantidad.setText(Integer.toString(lote.getCantidad()));
        campoVencimiento.setText(lote.getFechaVencimiento().getDate()+"-"+
                (lote.getFechaVencimiento().getMonth()+1)+"-"+
                (lote.getFechaVencimiento().getYear()+1900));

        campoElabora.setText(lote.getFechaElaboracion().getDate()+"-"+
                (lote.getFechaElaboracion().getMonth()+1)+"-"+
                (lote.getFechaElaboracion().getYear()+1900));

        areaPresentacion.setText(lote.getPresentacion());
    }//GEN-LAST:event_botonLotesActionPerformed


    public void llenarCombo(){
        alimentos=persistAlimento.buscarTodosAlimentos();
            for (int i = 0; i < alimentos.size(); i++) {
                this.comboAlimentos.addItem(alimentos.get(i).getNombre());
            }
    }

    public void llenarComboLotes(int idAlimento){
        comboLotes.removeAllItems();
        lotes=persistFreddy.lotesPorAlimento(idAlimento);
            for (int i = 0; i < lotes.size(); i++) {
                this.comboLotes.addItem(lotes.get(i).getIdLote());
            }
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaBusquedaLote().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea areaPresentacion;
    private javax.swing.JButton botonAlimentos;
    private javax.swing.JButton botonLotes;
    private javax.swing.JTextField campoCantidad;
    private javax.swing.JTextField campoElabora;
    private javax.swing.JTextField campoVencimiento;
    private javax.swing.JComboBox comboAlimentos;
    private javax.swing.JComboBox comboLotes;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
